package com.ywl.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.ywl.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.ywl.gulimall.product.entity.AttrEntity;
import com.ywl.gulimall.product.service.CategoryService;
import com.ywl.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ywl.gulimall.product.entity.AttrGroupEntity;
import com.ywl.gulimall.product.service.AttrGroupService;
import com.ywl.common.utils.PageUtils;
import com.ywl.common.utils.R;


/**
 * 属性分组
 *
 * @author ywl
 * @email xxx@gmail.com
 * @date 2022-02-05 21:09:57
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;

    // product/attrgroup/1/attr/relation
    @GetMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrEntity> list = attrGroupService.getAttrRelation(attrGroupId);
        return R.ok().put("data", list);
    }

    /**
     * /product/attrgroup/{catelogId}/withattr
     */
    @GetMapping("{catelogId}/withattr")
    public R listWithAttrs(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrsVo> vos =attrGroupService.listWithAttrs(catelogId);
        return R.ok().put("data", vos);
    }

    /**
     * 查询当前分组未关联的属性（排除当前分类下所有分组已经关联的属性）
     * @param attrGroupId
     * @param params
     * @return
     */
    // /product/attrgroup/1/noattr/relation
    @GetMapping("{attrGroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrGroupId") Long attrGroupId,
                            @RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.getAttrNoRelation(params, attrGroupId);
        return R.ok().put("page", page);
    }

    // /product/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R deleteAttrRelation(@RequestBody List<AttrAttrgroupRelationEntity> relationEntities) {
        attrGroupService.deleteAttrRelation(relationEntities);
        return R.ok();
    }

    // /product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R saveAttrRelation(@RequestBody List<AttrAttrgroupRelationEntity> relationEntities) {
        attrGroupService.saveAttrRelation(relationEntities);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);

        PageUtils queryPage = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", queryPage);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        // 设置所属分类的层级路径
        Long[] catelogPath = categoryService.findCatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catelogPath);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
